
/* --------------------------------------- Include Files ---------------------------------------- */
#include "stm32f10x.h"
#include "system_timer.h"
#include "driver.h"
#include "crc16.h"
#include "parameter.h"

/* ---------------------------------------- Macro Defines --------------------------------------- */

/* -------------------------------------- Data Type Defines -------------------------------------- */
typedef struct
{
    uint32_t    pk;
    uint32_t    offset;
    uint8_t     permission;
    uint8_t     lock;
    uint16_t    length;
    uint8_t    *pdata;
} DBS_TABLE_ITEM;


/* --------------------------------- Global Function Prototypes --------------------------------- */
uint32_t Dbs_initParTable(void);
uint32_t Dbs_setParDefault(void);
uint32_t Dbs_getParLength(uint32_t pk, uint32_t *length);
uint32_t Dbs_setParValue(uint32_t pk, uint8_t *data, uint32_t length);
uint32_t Dbs_getParValue(uint32_t pk, uint8_t *data, uint32_t *length);
uint32_t Dbs_getParLength(uint32_t pk, uint32_t *length);

/* -------------------------------------- Static Variables -------------------------------------- */
const uint32_t dbs_par_memstart = 10;
/*const uint32_t dbs_par_memend = 256;
const uint32_t dbs_app_memstart = dbs_par_memend;
const uint32_t dbs_app_memend = dbs_app_memstart + 5*1024;*/
const uint32_t dbs_par_rows = dbs_par_pk_number;
const uint32_t dbs_par_memsize = 128;
const uint32_t dbs_par_amemstart = dbs_par_memstart;
const uint32_t dbs_par_bmemstart = dbs_par_amemstart + dbs_par_memsize;
volatile uint8_t dbs_par_memory[dbs_par_memsize];

const uint8_t dbs_parinit_crc[2]                            = {0X00, 0X00};
const uint8_t dbs_parinit_isinit[1]                         = {0X00};
const uint8_t dbs_parinit_data1[1]                          = {0x01};  
const uint8_t dbs_parinit_data2[1]                          = {0x00};  
const uint8_t dbs_parinit_data3[1]                          = {0x02};  
const uint8_t dbs_parinit_data4[9]                          = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38};
const uint8_t dbs_parinit_data5[2]                          = {0x30,0x31};


volatile DBS_TABLE_ITEM dbs_par_format[] =
{
    {dbs_par_pk_crc,                            0,        0x10,     0,     2, (uint8_t *)dbs_parinit_crc},
    {dbs_par_pk_isinit,                         2,        0x10,     0,     1, (uint8_t *)dbs_parinit_isinit},
    {dbs_par_pk_data1,                          3,        0x10,     0,     1, (uint8_t *)dbs_parinit_data1},
    {dbs_par_pk_data2,                          4,        0x10,     0,     1, (uint8_t *)dbs_parinit_data2},
    {dbs_par_pk_data3,                          5,        0x10,     0,     1, (uint8_t *)dbs_parinit_data3},
    {dbs_par_pk_data4,                          6,        0x10,     0,     9, (uint8_t *)dbs_parinit_data4},
    {dbs_par_pk_data5,                         15,        0x10,     0,     2, (uint8_t *)dbs_parinit_data5}    
};

/**
  * @brief Dbs_initParTable
  * @param  
  * @retval 
  * @note   
  */
uint32_t Dbs_initParTable()
{
    uint32_t counter = 0;
    uint32_t n = 0;
    uint16_t data = 0;
    uint8_t temp0 = 0;
    uint8_t temp1 = 0;

    Epm_readData(dbs_par_amemstart + dbs_par_format[dbs_par_pk_isinit].offset, &temp0,1); 
    Epm_readData(dbs_par_bmemstart + dbs_par_format[dbs_par_pk_isinit].offset, &temp1,1);

    if ((temp0 != 0X5A) && (temp1 != 0x5A))
    {
        for (counter = 0; counter < dbs_par_rows; counter++)
        {
            for (n = 0; n < dbs_par_format[counter].length; n++)
            {
                dbs_par_memory[dbs_par_format[counter].offset + n] = *(dbs_par_format[counter].pdata + n);
            }
        }
        dbs_par_memory[dbs_par_format[dbs_par_pk_isinit].offset] = 0X5A;
        data  = Crc_cal((uint8_t *)(dbs_par_memory + 2), dbs_par_memsize - 2);
        dbs_par_memory[0] = (uint8_t)(data & 0XFF);
        dbs_par_memory[1] = (uint8_t)((data >> 8) & 0XFF);
        for (counter = 0; counter < dbs_par_memsize; counter++)
        {
            Epm_wrtieData(dbs_par_amemstart + counter, dbs_par_memory[counter]);
            Epm_wrtieData(dbs_par_bmemstart + counter, dbs_par_memory[counter]);
        }
    }

    for (counter = 0; counter < dbs_par_memsize; counter++)
    {
        Epm_readData(dbs_par_amemstart + counter, (uint8_t *)(dbs_par_memory + counter),1);
    }

    data  = Crc_cal((uint8_t *)(dbs_par_memory + 2), dbs_par_memsize - 2);
    if (((uint8_t)(data & 0XFF) == dbs_par_memory[0]) && ((uint8_t)((data >> 8) & 0XFF) == dbs_par_memory[1]))
    {
        temp0 = 1;
    }
    else
    {
        temp0 = 0;
    }

    for (counter = 0; counter < dbs_par_memsize; counter++)
    {
        Epm_readData(dbs_par_bmemstart + counter, (uint8_t *)(dbs_par_memory + counter),1); 
    }
    data  = Crc_cal((uint8_t *)(dbs_par_memory + 2), dbs_par_memsize - 2);

    if (((uint8_t)(data & 0XFF) == dbs_par_memory[0]) && ((uint8_t)((data >> 8) & 0XFF) == dbs_par_memory[1]))
    {
        temp1 = 1;
    }
    else
    {
        temp1 = 0;
    }

    if ((temp0 == 1) && (temp1 == 1))
    {
        return 1;
    }
    else if ((temp0 != 1) && (temp1 != 1))
    {
        return 0;
    }
    else if ((temp0 == 1) && (temp1 != 1))
    {
        for (counter = 0; counter < dbs_par_memsize; counter++)
        {
            Epm_readData(dbs_par_amemstart + counter, (uint8_t *)(dbs_par_memory + counter),1);  
        }
        for (counter = 0; counter < dbs_par_memsize; counter++)
        {
            Epm_wrtieData(dbs_par_bmemstart + counter, dbs_par_memory[counter]); 
        }
        return 1;
    }
    else if ((temp0 != 1) && (temp1 == 1))
    {
        for (counter = 0; counter < dbs_par_memsize; counter++)
        {
            Epm_readData(dbs_par_bmemstart + counter, (uint8_t *)(dbs_par_memory + counter),1);
        }
        for (counter = 0; counter < dbs_par_memsize; counter++)
        {
            Epm_wrtieData(dbs_par_amemstart + counter, dbs_par_memory[counter]); 
        }
        return 1;
    }
}

/**
  * @brief Dbs_setParDefault
  * @param  
  * @retval 
  * @note   
  */
uint32_t Dbs_setParDefault()
{
    Epm_wrtieData(dbs_par_amemstart + dbs_par_format[dbs_par_pk_isinit].offset, 0X00);
    Epm_wrtieData(dbs_par_bmemstart + dbs_par_format[dbs_par_pk_isinit].offset, 0X00);
    
    return Dbs_initParTable();
}

/**
  * @brief Dbs_setParValue
  * @param  
  * @retval 
  * @note   
  */
uint32_t Dbs_setParValue(uint32_t pk, uint8_t *data, uint32_t length)
{
    uint32_t counter = 0;
    uint16_t temp = 0;

    if (pk >= dbs_par_rows)
    {
        return 0;
    }

    if (dbs_par_format[pk].lock == 1)
    {
        return 0;
    }

    if (dbs_par_format[pk].permission == 0x20)
    {
        return 0;
    }

    if (dbs_par_format[pk].length < length)
    {
        return 0;
    }

    /* 写入数据 */
    if (length == 0)
    {
        length = dbs_par_format[pk].length;
        for (counter = 0; counter < length; counter++)
        {
            dbs_par_memory[dbs_par_format[pk].offset + counter] = 0x00;
            Epm_wrtieData(dbs_par_amemstart + dbs_par_format[pk].offset + counter, 0x00);
            Epm_wrtieData(dbs_par_bmemstart + dbs_par_format[pk].offset + counter, 0x00);
        }
    }
    else
    {
        for (counter = 0; counter < length; counter++)
        {
            dbs_par_memory[dbs_par_format[pk].offset + counter] = *(data + counter);
            Epm_wrtieData(dbs_par_amemstart + dbs_par_format[pk].offset + counter, *(data + counter));
            Epm_wrtieData(dbs_par_bmemstart + dbs_par_format[pk].offset + counter, *(data + counter));
        }
        if (dbs_par_format[pk].length > length)
        {
            dbs_par_memory[dbs_par_format[pk].offset + length] = 0x00;
            Epm_wrtieData(dbs_par_amemstart + dbs_par_format[pk].offset + length, 0x00);
            Epm_wrtieData(dbs_par_bmemstart + dbs_par_format[pk].offset + length, 0x00);
        }
    }

    temp  = Crc_cal((uint8_t *)(dbs_par_memory + 2), dbs_par_memsize - 2);
    dbs_par_memory[0] = (uint8_t)(temp & 0XFF);
    dbs_par_memory[1] = (uint8_t)((temp >> 8) & 0XFF);
    
    Epm_wrtieData(dbs_par_amemstart + 0, (uint8_t)(temp & 0XFF));
    Epm_wrtieData(dbs_par_amemstart + 1, (uint8_t)((temp >> 8) & 0XFF));
    Epm_wrtieData(dbs_par_bmemstart + 0, (uint8_t)(temp & 0XFF));
    Epm_wrtieData(dbs_par_bmemstart + 1, (uint8_t)((temp >> 8) & 0XFF));

    return 1;
}


/**
  * @brief Dbs_getParValue
  * @param  
  * @retval 
  * @note   
  */
uint32_t Dbs_getParValue(uint32_t pk, uint8_t *data, uint32_t *length)
{
    uint32_t counter = 0;

    if (pk >= dbs_par_rows)
    {
        return 0;
    }

    if (dbs_par_format[pk].lock == 1)
    {
        return 0;
    }

    if (dbs_par_format[pk].permission == 0x30)
    {
        return 0;
    }

    *length = dbs_par_format[pk].length;
    for (counter = 0; counter < dbs_par_format[pk].length; counter++)
    {
        *(data + counter) = dbs_par_memory[dbs_par_format[pk].offset + counter];
    }

    return 1;
}

/**
  * @brief Dbs_getParLength
  * @param  
  * @retval 
  * @note   
  */
uint32_t Dbs_getParLength(uint32_t pk, uint32_t *length)
{
    if (pk >= dbs_par_rows)
    {
        return 0;
    }

    *length = dbs_par_format[pk].length;
    return 1;
}








